/*
 * Copyright (C) 2010 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.mjl.util;

import static com.google.common.base.Preconditions.*;

import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;

import com.google.common.collect.*;

public class Annotations {

	private Annotations() {
	}

	public static final <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationType) {
		A annotation = element.getAnnotation(annotationType);

		if (annotation == null) {
			for (Annotation a : element.getAnnotations()) {
				annotation = getAnnotationOnAnnotation(a, annotationType);
				if (annotation != null) {
					break;
				}
			}
		}

		return annotation;
	}

	@SuppressWarnings("unchecked")
	public static final <A extends Annotation> List<A> getParameterAnnotations(Method method, Class<A> annotationType) {
		Annotation[][] parameterAnnotations = method.getParameterAnnotations();
		List<A> annotations = new ArrayList<A>(parameterAnnotations.length);

		for (Annotation[] parameterAnnotation : parameterAnnotations) {
			A aux = null;
			for (Annotation a : parameterAnnotation) {
				if (a.annotationType() == annotationType) {
					aux = (A) a;
					break;
				}
				aux = getAnnotationOnAnnotation(a, annotationType);
				if (aux != null) {
					break;
				}
			}
			annotations.add(aux);
		}

		return Collections.unmodifiableList(annotations);
	}

	public static final <A extends Annotation> Map<String, Object> getMergedAnnotationAttributes(AnnotatedElement element, Class<A> annotationType) {
		Map<String, Object> map = Maps.newHashMap();
		A annotation = element.getAnnotation(annotationType);

		if (annotation == null) {
			for (Annotation a : element.getAnnotations()) {
				annotation = getAnnotationOnAnnotation(a, annotationType);
				if (annotation != null) {
					attributesToMap(annotation, map);
					attributesToMap(a, map);
					break;
				}
			}
		} else {
			attributesToMap(annotation, map);
		}

		return map;
	}

	public static final <A extends Annotation> void attributesToMap(A annotation, Map<String, Object> map) {
		Method[] methods = annotation.getClass().getDeclaredMethods();
		for (Method method : methods) {
			if (!JavaBeans.isObjectMethod(method) && !"annotationType".equals(method.getName())) {
				try {
					map.put(method.getName(), method.invoke(annotation));
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
	}

	public static final <A extends Annotation> A getAnnotationOnAnnotation(Annotation annotation, Class<A> annotationType) {
		return annotation.annotationType().getAnnotation(annotationType);
	}

	public static final void checkForRuntimeRetention(Class<? extends Annotation> annotationType) {
		Retention retention = annotationType.getAnnotation(Retention.class);
		checkArgument(retention != null && retention.value() == RetentionPolicy.RUNTIME, "Annotation " + annotationType.getSimpleName() + " is missing RUNTIME retention");
	}

}
